package com.lee.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lee.base.model.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/3/9
 */
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DocOperation {

    private static RestHighLevelClient client;
    private static ObjectMapper objectMapper;
    @BeforeAll
    public static void init() {
        client = new RestHighLevelClient(RestClient.builder(HttpHost.create("https://elasticsearch-dev.souban.io")));
        objectMapper = new ObjectMapper();
    }
    @Test
    @Order(1)
    public void insert() throws IOException {
        User user = User.builder()
                .name("小米")
                .gender("男")
                .age(10)
                .build();
        String userJson = objectMapper.writeValueAsString(user);
        IndexRequest request = new IndexRequest("user").id("1008").source(userJson, XContentType.JSON);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    @Test
    @Order(2)
    public void update() throws IOException {
        UpdateRequest request = new UpdateRequest().index("user").id("1001");
        request.doc(XContentType.JSON, "gender", "女");
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse.getResult());
    }

    @Test
    @Order(4)
    public void delete() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.index("user");
        deleteRequest.id("1001");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.getResult());
    }

    @Test
    @Order(3)
    public void query() throws IOException {
        GetRequest getRequest = new GetRequest();
        getRequest.index("user");
        getRequest.id("1001");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());
    }

    @AfterAll
    private static void destory() throws IOException {
        client.close();
    }
}
